package com.shuma.auth.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.shuma.auth.entity.RestBean;
import com.shuma.auth.model.dto.*;
import com.shuma.auth.service.ProductBaseService;
import com.shuma.auth.utils.Const;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author lele
 */
@Slf4j
@RestController
@RequestMapping("product")
public class ProductBaseController {

    @Autowired
    private ProductBaseService productBaseService;

    /**
     * 分页模糊查询
     * @param pageParams
     * @param queryProductParamsDto
     * @return
     */
        //json
//    @PostMapping("/pageSearch")
//    public RestBean<Page<ProductDto>> pageList(PageParams pageParams,
//                                               @RequestBody(required = false) QueryProductParamsDto queryProductParamsDto,
//                                               @RequestAttribute(Const.ATTR_USER_ID) Long id){
//
//        Page<ProductDto> list = productBaseService.pageProduct(pageParams,queryProductParamsDto,id);
//        return RestBean.success(list);
//    }
    //表单接受
    @PostMapping("/pageSearch")
    public RestBean<Page<ProductDto>> pageList(PageParams pageParams,
                                                QueryProductParamsDto queryProductParamsDto,
                                               @RequestAttribute(Const.ATTR_USER_ID) Long id){

        Page<ProductDto> list = productBaseService.pageProduct(pageParams,queryProductParamsDto,id);
        return RestBean.success(list);
    }

    /**
     * 保存或暂存
     * @param operaterId
     * @param addProductDto
     * @return
     */
    @PostMapping("/{operaterId}/createProduct")
    public RestBean<String> saveProduct(@PathVariable Integer operaterId,
                                        @RequestBody AddProductDto addProductDto,
                                        @RequestAttribute(Const.ATTR_USER_ID) Long id){
        //暂存
        if(operaterId==0){
            return productBaseService.tempSaveProduct(addProductDto,id);
        }
        //保存
        else if(operaterId==1){
            return productBaseService.foreverSaveProduct(addProductDto,id);
        }
        return null;
    }

    /**
     * 编辑商品
     * @param editProductDto
     * @return
     */
    @PutMapping("/editProduct")
    public RestBean<Void> editProduct(@RequestBody ProductInfoDto editProductDto,
                                      @RequestAttribute(Const.ATTR_USER_ID) Long id){

        return productBaseService.editProduct(editProductDto,id);
    }

    /**
     * 根据商品id查商品
     * @param id
     * @return
     */
    @GetMapping("{id}")
    public RestBean<ProductInfoDto> selectProductInfoById(@PathVariable("id") Long productId,
                                                          @RequestAttribute(Const.ATTR_USER_ID) Long id){
        ProductInfoDto productInfoDto = productBaseService.selectProductInfoById(productId,id);
        return RestBean.success(productInfoDto);
    }


    /**
     * 提交审核1
     * @param id
     * @return
     */
    @GetMapping("submitForReview/{id}")
    public RestBean<Void> submitProduct(@PathVariable("id") Long productId,@RequestAttribute(Const.ATTR_USER_ID) Long id){
        return productBaseService.submitForReview(productId,id);
    }

    /**
     * 预览1
     * @param id
     * @return
     */
    @GetMapping("/preview/{id}")
    public RestBean<ProductPreviewDto> previewProduct(@PathVariable("id") Long productId,
                                                      @RequestAttribute(Const.ATTR_USER_ID) Long id){
        ProductPreviewDto productPreviewDto=productBaseService.preview(productId,id);
        return RestBean.success(productPreviewDto);
    }

    /**
     * 批量上线1
     * @param ids
     * @return
     */
    @PostMapping("batchesOnline")
    public RestBean<Void> batchesOnlineProduct(@RequestParam List<Long> ids,@RequestAttribute(Const.ATTR_USER_ID) Long id){

        return productBaseService.batchesOnlineProduct(ids,id);
    }
    /**
     * 批量下线1
     * @param ids
     * @return
     */
    @PostMapping("batchesOffline")
    public RestBean<Void> batchesOfflineProduct(@RequestParam List<Long> ids,@RequestAttribute(Const.ATTR_USER_ID) Long id){
        return productBaseService.batchesOfflineProduct(ids,id);
    }

    /**
     * 将这个商品id审核通过1
     * @param id
     * @return
     */
    @GetMapping("audit/approved/{id}")
    public RestBean<Void> approvedProduct(@PathVariable("id") Long productId,@RequestAttribute(Const.ATTR_USER_ID) Long id){
        return productBaseService.approvedProduct(productId,id);
    }

    /**
     * 将这个商品id审核驳回1
     * @param id
     * @return
     */
    @GetMapping("audit/refuse/{id}")
    public RestBean<Void> refuseProduct(@PathVariable("id") Long productId,@RequestAttribute(Const.ATTR_USER_ID) Long id){
        return productBaseService.refuseProduct(productId,id);
    }

}
